﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BaseData;
using WaterRevenueMS.Core.Repository.BaseData;
using WaterRevenueMS.Domain.ViewModels.BaseData;
using WaterRevenueMS.Componnent.Entities.BaseData;
using  WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IMeterBookService))]
    public class MeterBookService : CoreServiceBase, IMeterBookService
    {
        [Import]
        public IMeterBookRepository MeterBookRepository { get; set; }

        public IQueryable<Componnent.Entities.BaseData.MeterBook> MeterBooks
        {
            get { return MeterBookRepository.Entities; }
        }

        public Componnent.Common.OperationResult Insert(Domain.ViewModels.BaseData.MeterBook.MeterBookModel model)
        {
            var entity = new MeterBook
            {

                Qu_YSZ_No = model.AreaId != 0 ? model.AreaId : null,
            
                Biao_CMC = model.Biao_CMC,
                Biao_CBH = model.Biao_CBH,
                CreateDT = DateTime.Now,
                ReviseDT = DateTime.Now 

            };
            MeterBookRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public Componnent.Common.OperationResult Update(Domain.ViewModels.BaseData.MeterBook.MeterBookModel model)
        {
            var entity = new MeterBook
            {
                
                Qu_YSZ_No = model.AreaId != 0 ? model.AreaId : null,

                Biao_CMC = model.Biao_CMC,
                Biao_CBH = model.Biao_CBH,
                CreateDT = DateTime.Now,
                Id = model.id,
                ReviseDT = DateTime.Now 

            };
            MeterBookRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public Componnent.Common.OperationResult Delete(int Id)
        {
            var model = MeterBooks.FirstOrDefault(t => t.Id == Id);
            model.IsDeleted = true;

            MeterBookRepository.Delete(model);
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}
